<?php

namespace MarketServer\Model;

/**
 * PermissionModel数据库模型
 *
 * @since 1.0.0 <2015-3-23> SoChishun <14507247@qq.com> Added.
 */
class PermissionModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tuser_permission';

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'main_nav' => array(
            'field' => 'id, name as text, link_url',
            'where' => array('is_menu' => 1)
        ),
        'all_tree' => array(
            'field' => 'id, name as text, link_url',
        )
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 返回用户菜单
     * @param int $role_id
     * @return array
     * @since 1.0 <2015-7-8> SoChishun Added.
     */
    public function get_user_menu($role_id, $expire = 15) {
        if (!$role_id) {
            return array('status' => false, 'info' => '角色未分配');
        }
        $permission_rule = $this->cache($expire)->table('tuser_role')->where(array('id' => $role_id))->getField('permission_rule');
        if (!$permission_rule) {
            return array('status' => false, 'info' => '角色权限未分配');
        }
        $skey = 'menuset_cti_' . $role_id;
        $list = S($skey);
        if ($list) {
            return array('status' => true, 'info' => $list);
        }
        $fields = 'id, name, code, link_url as url';
        $where = ('all' == $permission_rule) ? '' : " and id in ($permission_rule)";
        $list = $this->cache($expire)->where('parent_id=0 and is_menu=1' . $where)->field($fields)->select();
        if (!$list) {
            return array('status' => false, 'info' => '没有菜单数据');
        }
        foreach ($list as &$row) {
            $list2 = $this->cache($expire)->where('parent_id=' . $row['id'] . $where . ' and is_menu=1')->field($fields)->select();
            if ($list2) {
                foreach ($list2 as &$row2) {
                    $list3 = $this->cache($expire)->where('parent_id=' . $row2['id'] . $where . ' and is_menu=1')->field($fields)->select();
                    if ($list3) {
                        foreach ($list3 as &$row3) {
                            $list4 = $this->cache($expire)->where('parent_id=' . $row3['id'] . $where . ' and is_menu=1')->field($fields)->select();
                            if ($list4) {
                                $row3['children'] = $list4;
                            }
                        }
                        $row2['children'] = $list3;
                    }
                }
                $row['children'] = $list2;
            }
        }
        S($skey, $list, array('expire' => $expire));
        return array('status' => true, 'info' => $list);
    }

    /**
     * 返回用户历史菜单
     * @param int $user_id
     * @return array
     * @since 1.0 <2015-7-8> SoChishun Added.
     */
    public function get_menu_history($user_id) {
        $str = cookie(C('USER.USER_KEY') . 'mnh_' . $user_id);
        if (!$str) {
            return $this->returnMsg(false, '无历史记录');
        }
        $arr = (Array) json_decode($str);
        $ids = $arr['ids'];
        sort($ids);
        $str = implode(',', $ids);
        $fields = 'id, name, code, link_url as url';
        $menus = $this->cache(true)->where("id in ($str)")->field($fields)->select();
        return $this->returnMsg($menus, $menus ? $menus : '没有历史菜单');
    }

    public function get_tree_list($check_ids) {
        return $this->base_get_tree_list('all_tree', $check_ids);
    }

}
